var currentPage = 0
/*
currentSearch == -1 权限全部搜索
currentSearch == -2 权限条件搜索/
 */
var currentSearch = -1

$(document).ready(function () {
    addTimeOutAction()
    initInterfaceWithPermission()
    $('#select-all').on('click', function () {
        if(this.checked == true){
            $('.select-sub-box').prop('checked',true)
        }
        else{
            $('.select-sub-box').prop('checked',false)
        }
    })

    getAllDepartmentInformation()

    /*
    搜索添加回车绑定事件/
     */
    $('#search-departmentID').on('keypress', function (event) {
        if(event.keyCode == '13'){
            searchDepartmentByID()
        }
    })
})

/*
权限初始化界面
 */
function initInterfaceWithPermission() {
    if($.inArray(14, JSON.parse(window.localStorage.permissions)) < 0){
        $('#ManageDepartment-menu').addClass('hidden')
        $('#ManageJobNature-menu').addClass('hidden')
        $('#ManageNation-menu').addClass('hidden')
        $('#ManageEducation-menu').addClass('hidden')
        $('#ManageHealthStatus-menu').addClass('hidden')
        $('#ManageMaritalStatus-menu').addClass('hidden')
        $('#ManageMilitaryStatus-menu').addClass('hidden')
        $('#ManagePoliticalStatus-menu').addClass('hidden')
        $('#ManagePersonalContractType-menu').addClass('hidden')
        $('#ManageContractStatus-menu').addClass('hidden')
        $('#ManageRole-menu').addClass('hidden')
        $('#ManageResignType-menu').addClass('hidden')
        $('#ManagePermissions-menu').addClass('hidden')
        $('.right-panel').addClass('hidden')
    }
}

/*
/添加部门
 */
function addDepartment() {
    var departmentName = $('#modal-departmentName').val()
    var departmentShortName = $('#modal-departmentShortName').val()
    var departmentDescription = $('#modal-departmentDescription').val()
    if(!departmentName){
        alert("请输入部门名称！")
        return
    }else{
        var urlStr = ipPort + '/department/add?name=' + departmentName + '&shortName=' + departmentShortName + '&description=' + departmentDescription
        $.ajax({
            url:urlStr,
            dataType:'json',
            success:function (obj) {
                if(obj.code == 8){
                    alert('新增失败, 部门简称只能是唯一的2位字母！')
                }else{
                    alert(obj.message)
                    getAllDepartmentInformation()
                }
            },
            error:function (error) {
                console.log(error)
            }
        })
    }
}
/*
设置修改面板信息/
 */
function setModifyModalInformation(obj) {
    var td = $(obj).parent().parent().find('td')
    $('#modal-modifyDepartmentID').val(td.eq(0).text())
    $('#modal-modifyDepartmentName').val(td.eq(1).text())
    $('#modal-modifyDepartmentShortName').val(td.eq(2).text())
    $('#modal-modifyDepartmentDescription').val(td.eq(3).text())
}
/*
修改部门信息/
 */
function modifyDepartment() {
    var departmentID = $('#modal-modifyDepartmentID').val()
    var departmentName = $('#modal-modifyDepartmentName').val()
    var departmentShortName = $('#modal-modifyDepartmentShortName').val()
    var departmentDescription = $('#modal-modifyDepartmentDescription').val()
    var urlStr = ipPort + '/department/update?id='+ departmentID + "&name=" + departmentName + "&shortName=" + departmentShortName + "&description=" + departmentDescription
    $.ajax({
        url:urlStr,
        dataType:'json',
        success:function (obj) {
            if(obj.code == 8){
                alert('修改失败, 部门简称只能是唯一的2位字母！')
            }
            else if(obj.code == 0){
                alert("修改部门信息成功！")
                getAllDepartmentInformation()
            }
        },
        error:function (error) {
            console.log(error)
        }
    })
}
/*
删除部门信息/
 */
function deleteDepartment(thisObj) {
    var td = $(thisObj).parent().parent().find('td')
    var departmentID = td.eq(0).text()
    var urlStr = ipPort + '/department/deleteById?id='+ departmentID
    $.ajax({
        url:urlStr,
        dataType:'json',
        success:function (obj) {
            if(obj.code == 0){
                alert("删除部门信息成功！")
                getAllDepartmentInformation()
            }else {
                alert(obj.message)
            }
        },
        error:function (error) {
            console.log(error)
        }
    })
}
/*
批量删除部门信息/
 */
function deleteDepartmentInBatch() {
    var select_sub_box = $('.select-sub-box')
    var jsonArr = []
    for(var i = 0; i < select_sub_box.length; i++){
        if(select_sub_box.eq(i).is(':checked') == true){
            var json = {}
            json['id'] = parseInt(select_sub_box.eq(i).attr('value'));
            jsonArr.push(json)
        }
    }
    let myjson = JSON.stringify(jsonArr)
    var urlStr = ipPort + '/department/deleteByIdBatch'
    $.ajax({
        url:urlStr,
        contentType:'application/json',
        data:myjson,
        dataType:'json',
        type:'post',
        success:function (obj) {
            if(obj.code == 0){
                alert("删除部门信息成功！")
                getAllDepartmentInformation()
            }else {
                alert(obj.message)
            }
        },
    error:function (error) {
        console.log(error)
    }
    })
}
/*
通过ID搜索部门信息/
 */
function searchDepartmentByID(page_ = 0) {
    var departmentName = $('#search-departmentID').val()
    if(departmentName == ''){
        alert("请输入部门名称！")
        return
    }
    currentSearch = -2
    currentPage = page_
    var urlStr = ipPort + '/department/getByNameLikeByPage?name='+ departmentName + "&page=" + currentPage
    $.ajax({
        url:urlStr,
        dataType:'json',
        success:function (obj) {
            if(obj.code == 0){
                setDepartmentTableInformation(obj)
            }else{
                alert(obj.message)
            }
        },
        error:function (error) {
            console.log(error)
        }
    })
}
/*
获取全部部门信息/
 */
function getAllDepartmentInformation(page_ = 0) {
    currentSearch = -1
    currentPage = page_
    var size = 10
    var sortFieldName = 'id'
    var asc = 1
    var urlStr = ipPort + '/department/getAllByPage?page='+ currentPage + '&size=' + size + '&sortFieldName=' + sortFieldName + '&asc=' + asc
    $.ajax({
        url:urlStr,
        dataType:'json',
        success:function (obj) {
            setDepartmentTableInformation(obj)
        },
        error:function (error) {
            console.log(error)
        }
    })
}
/*
设置部门table信息/
 */
function setDepartmentTableInformation(obj) {
    $('#allDepartmentInformationPanel .currentPage').text(currentPage + 1)
    $('#allDepartmentInformationPanel .totalPage').text(obj.data.totalPages)
    var table_tr = $('.table-tr')
    var department_id = $('.department-id')
    var department_name = $('.department-name')
    var department_shortName = $('.department-shortName')
    var department_description = $('.department-description')
    for(var i = 0; i < obj.data.numberOfElements; i++){
        table_tr.eq(i).removeClass('hidden')
        department_id.eq(i).html("<input class=\"select-box select-sub-box\" type=\"checkbox\"" +  "value=\"" + obj.data.content[i].id + "\"" + ">" + obj.data.content[i].id)
        department_name.eq(i).text(obj.data.content[i].name)
        department_shortName.eq(i).text(obj.data.content[i].shortName)
        department_description.eq(i).text(obj.data.content[i].description)
    }
    for (var i = obj.data.numberOfElements; i < 10; i++){
        table_tr.eq(i).addClass('hidden')
    }
}

/*
上一页/
 */
function previousPage(str) {
    var currentPage_ = $(str).find('.currentPage').text()
    if(currentPage_ == 1){
        alert("已经是第一页！")
        return
    }
    currentPage--
    if(currentPage < 0){
        currentPage = 0
    }
    if(currentSearch == -1){
        getAllDepartmentInformation(currentPage)
    }else  if(currentSearch == -2){
        searchDepartmentByID(currentPage)
    }

}
/*
下一页/
 */
function nextPage(str) {
    var currentPage_ = $(str).find('.currentPage').text()
    var totalPage_ = $(str).find('.totalPage').text()
    if(currentPage_ == totalPage_){
        alert("已经是最后一页！")
        return
    }
    currentPage++
    if(currentSearch == -1){
        getAllDepartmentInformation(currentPage)
    }else  if(currentSearch == -2){
        searchDepartmentByID(currentPage)
    }




}
/*
跳转页/
 */
function skipPage(str) {
    var skipPage_ = parseInt( $(str).find('.skipPage').val())
    var totalPage_ = parseInt( $(str).find('.totalPage').text())
    if(skipPage_ - totalPage_ > 0){
        alert("没有此页！")
        return
    }
    if(skipPage_ < 1){
        alert("没有此页！")
        return
    }
    currentPage = skipPage_ - 1
    if(currentSearch == -1){
        getAllDepartmentInformation(currentPage)
    }else  if(currentSearch == -2){
        searchDepartmentByID(currentPage)
    }
}